package com.yj.ccs.abs.web.management.controller;

import com.yj.ccs.abs.boot.authorization.annotation.CurrentUser;
import com.yj.ccs.abs.boot.authorization.annotation.UserRole;
import com.yj.ccs.abs.boot.pagination.Page;
import com.yj.ccs.abs.common.dto.IdDto;
import com.yj.ccs.abs.common.enums.UserRoleType;
import com.yj.ccs.abs.common.model.BaseResult;
import com.yj.ccs.abs.common.service.PasswordService;
import com.yj.ccs.abs.web.account.service.UserService;
import com.yj.ccs.abs.web.management.dto.*;
import com.yj.ccs.abs.web.management.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * Copyright 2018 (C) Yunjian-VC
 * Created on : 2018/3/14
 * Author     : Unbelievable9
 **/
@Api(tags = "Web-Account-Controller")
@RestController
@RequestMapping("/web/account")
public class AccountController {

    private final UserService userService;

    private final PasswordService passwordService;

    @Autowired
    public AccountController(UserService userService,
                             PasswordService passwordService) {
        this.userService = userService;
        this.passwordService = passwordService;
    }

    @ApiOperation("账户列表")
    @GetMapping("/list")
    @UserRole(UserRoleType.ORIGINATOR)
    public ResponseEntity<BaseResult> list(KeywordDto KeywordDto, @CurrentUser User user) {
        Page<User> userPage = userService.getUserPage(KeywordDto);

        return new ResponseEntity<>(BaseResult.success("账户列表获取成功").setData(userPage), HttpStatus.OK);
    }

    @ApiOperation("检测账号是否存在")
    @GetMapping("/check-account")
    @UserRole(UserRoleType.ORIGINATOR)
    public ResponseEntity<BaseResult> checkAccount(@Validated CheckAccountDto checkAccountDto, @CurrentUser User user) {
        Boolean existed = userService.accountExist(checkAccountDto.getAccount());

        if (existed) {
            return new ResponseEntity<>(BaseResult.error("账号已存在"), HttpStatus.OK);
        } else {
            return new ResponseEntity<>(BaseResult.success("账号不存在"), HttpStatus.OK);
        }
    }

    @ApiOperation("添加账户")
    @PostMapping("/create")
    @UserRole(UserRoleType.ORIGINATOR)
    public ResponseEntity<BaseResult> create(@RequestBody @Validated CreateUserDto createUserDto, @CurrentUser User user) {
        userService.createUser(createUserDto, user.getAccount());

        return new ResponseEntity<>(BaseResult.success("添加账户成功"), HttpStatus.OK);
    }

    @ApiOperation("账户详情")
    @GetMapping("/detail")
    @UserRole(UserRoleType.ORIGINATOR)
    public ResponseEntity<BaseResult> detail(@Validated IdDto idDto, @CurrentUser User user) {
        User theUser = userService.getById(idDto.getId());

        if (theUser == null) {
            return new ResponseEntity<>(BaseResult.error("账户不存在"), HttpStatus.OK);
        } else {
            return new ResponseEntity<>(BaseResult.success("账户详情获取成功").setData(theUser), HttpStatus.OK);
        }
    }

    @ApiOperation("编辑账户")
    @PostMapping("/update")
    @UserRole(UserRoleType.ORIGINATOR)
    public ResponseEntity<BaseResult> update(@RequestBody @Validated UpdateUserDto updateUserDto, @CurrentUser User user) {
        User theUser = userService.getById(updateUserDto.getId());

        if (theUser == null) {
            return new ResponseEntity<>(BaseResult.error("账户不存在"), HttpStatus.OK);
        } else {
            String existedAccount = userService.updateUser(updateUserDto, theUser);

            if (existedAccount != null) {
                return new ResponseEntity<>(BaseResult.error("账号已存在").setData(existedAccount), HttpStatus.OK);
            } else {
                return new ResponseEntity<>(BaseResult.success("编辑账户成功"), HttpStatus.OK);
            }
        }
    }

    @ApiOperation("/重置密码")
    @PostMapping("/reset-password")
    @UserRole(UserRoleType.ORIGINATOR)
    public ResponseEntity<BaseResult> resetPassword(@Validated @RequestBody AccountPasswordDto accountPasswordDto, @CurrentUser User user) {
        User theUser = userService.getById(accountPasswordDto.getId());

        if (theUser == null) {
            return new ResponseEntity<>(BaseResult.error("账户不存在"), HttpStatus.OK);
        } else {
            if (passwordService.isValidPassword(theUser.getPassword(), theUser.getPassword_salt(), accountPasswordDto.getPassword())) {
                return new ResponseEntity<>(BaseResult.error("修改后密码与原密码相同"), HttpStatus.OK);
            } else {
                userService.changeUserPassword(accountPasswordDto.getPassword(), accountPasswordDto.getId());
                return new ResponseEntity<>(BaseResult.success("重置密码成功"), HttpStatus.OK);
            }
        }
    }

    @ApiOperation("/修改账户状态")
    @PostMapping("/change-status")
    @UserRole(UserRoleType.ORIGINATOR)
    public ResponseEntity<BaseResult> changeStatus(@RequestBody @Validated ChangeStatusDto ChangeStatusDto, @CurrentUser User user) {
        User theUser = userService.getById(ChangeStatusDto.getId());

        if (theUser == null) {
            return new ResponseEntity<>(BaseResult.error("账户不存在"), HttpStatus.OK);
        } else {
            userService.changeUserStatus(ChangeStatusDto);

            return new ResponseEntity<>(BaseResult.success("修改账户状态成功"), HttpStatus.OK);
        }
    }

    @ApiOperation("/删除账户")
    @PostMapping("/delete")
    @UserRole(UserRoleType.ORIGINATOR)
    public ResponseEntity<BaseResult> delete(@RequestBody @Validated IdListDto idListDto, @CurrentUser User user) {
        userService.deleteUser(idListDto);

        return new ResponseEntity<>(BaseResult.success("删除账户成功"), HttpStatus.OK);
    }
}
